package org.dc.service;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import org.dc.entity.WorkDay;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @user: Vladimir Chernishov
 * @date: 3/8/12
 * @time: 1:37 PM
 */
public class WorkDayDaoImpl extends SQLiteOpenHelper implements WorkDayDao {

    private DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

    public WorkDayDaoImpl(Context context) {
        super(context, "workday.db", null, 4);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table workday (date integer primary key,\n" +
                "shift_id integer);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL("DROP TABLE IF EXISTS workday");
        onCreate(db);
    }


    @Override
    public void save(WorkDay workDay) {
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.query("workday", new String[]{"date"}, "date = ?",
                new String[]{dateFormat.format(workDay.getDate())}, null, null, null);
        if (cursor.moveToNext()) {
            ContentValues values = new ContentValues();
            values.put("shift_id", workDay.getShiftId());
            getWritableDatabase().update("workday", values, "date = ?", new String[]{dateFormat.format(workDay.getDate())});
        } else {
            ContentValues values = new ContentValues();
            values.put("date", dateFormat.format(workDay.getDate()));
            values.put("shift_id", workDay.getShiftId());
            db.insertOrThrow("workday", null, values);
        }
    }

    @Override
    public List<WorkDay> getWorkDaysForMonth(Calendar calendar) {
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        Date from = calendar.getTime();
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date till = calendar.getTime();

        return getWorkDaysFromTill(from, till);
    }

    private List<WorkDay> getWorkDaysFromTill(Date from, Date till) {
        List<WorkDay> ret = new ArrayList<WorkDay>();
        Cursor cursor = getReadableDatabase().query("workday", new String[]{"date, shift_id"}, "date BETWEEN ? AND ?",
                new String[]{dateFormat.format(from), dateFormat.format(till)}, null, null, "date");

        while(cursor.moveToNext()){
            try {
                WorkDay day = new WorkDay();
                day.setDate((dateFormat.parse(cursor.getString(0))));
                day.setShiftId(cursor.getInt(1));
                ret.add(day);
            } catch (ParseException e) {
                Log.w("ParseException", cursor.getString(0));
                Log.w("ParseException", e);
            }
        }
        return ret;
    }

    @Override
    public List<WorkDay> getWorkDaysForWeek(Calendar currentDate) {
        currentDate.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        Date from = currentDate.getTime();
        currentDate.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
        currentDate.add(Calendar.DAY_OF_YEAR, 1);
        Date till = currentDate.getTime();

        return getWorkDaysFromTill(from, till);
    }

    @Override
    public void clearSchedule() {
        getWritableDatabase().delete("workday", null, null);
    }
}
